package com.dotflat.staffExtend.service.impl;


import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.staffExtend.domain.StaffExtendInfoRank;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankCircle;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankLog;
import com.dotflat.staffExtend.po.StaffExtendInfoRankLogPO;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankCircleRepository;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankLogRepository;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankRepository;
import com.dotflat.staffExtend.service.StaffExtendInfoRankLogService;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class StaffExtendInfoRankLogServiceImpl  extends AreableObjectBaseServiceImpl<StaffExtendInfoRankLog, StaffExtendInfoRankLogRepository>
        implements StaffExtendInfoRankLogService {

    @Autowired
    @Override
    public void setBaseRepository(StaffExtendInfoRankLogRepository repository){
        super.setBaseRepository(repository);
    }

    @Autowired
    private StaffExtendInfoRankCircleRepository circleRepository;

    @Autowired
    private StaffExtendInfoRankRepository rankRepository;

    @Override
    @Transactional(rollbackOn = RuntimeException.class )
    public StaffExtendInfoRankLog add(String areaId, String staffId, AreableObjectBasePO param) {

        if (param instanceof StaffExtendInfoRankLogPO){
            StaffExtendInfoRankLogPO po = (StaffExtendInfoRankLogPO)param;

            StaffExtendInfoRank rank = rankRepository.findFirstByAreaIdAndRankCircleIdAndStaffId(areaId, po.getRankCircleId(), po.getStaffId());
            if (rank == null){
                rank = new StaffExtendInfoRank();
                rank.setAreaId(areaId);
                rank.setCreatedBy(po.getStaffId());
                rank.setStaffId(po.getStaffId());
                rank.setRankCircleId(po.getRankCircleId());
                StaffExtendInfoRankCircle circle = circleRepository.getOne(po.getRankCircleId());
                rank.setRankCircleName(circle.getTitle());
//                rankRepository.save(rank);
            }

            StaffExtendInfoRankLog result = baseRepository.findFirstByAreaIdAndStaffIdAndCreatedByAndRankCircleId(areaId, po.getStaffId(), staffId, po.getRankCircleId());
            if (result != null){
                throw new BusinessException(ErrorCode.MOBILE_IS_EXIST, "您已经给这个员工评过分了");
            }

            result = add(areaId, staffId, param, null);

            // 0:Staff    1:家长   2:学习记录   3:培训
            switch (result.getSourceType()){
                case 0:{
                    rank.setManagerRankCount(rank.getManagerRankCount() + 1);
                    rank.setManagerRankTotal(rank.getManagerRankTotal() + po.getRankVal());
                }
                    break;
                case 1:{
                    rank.setParentRankCount(rank.getParentRankCount() + 1);
                    rank.setParentRankTotal(rank.getParentRankTotal() + po.getRankVal());
                }
                    break;
                case 2: {
                    rank.setStudyRankCount(rank.getStudyRankCount() + 1);
                    rank.setStudyRankTotal(rank.getStudyRankTotal() + po.getRankVal());
                }
                    break;
                case 3:{
                    rank.setTrainingRankCount(rank.getTrainingRankCount() + 1);
                    rank.setTrainingRankTotal(rank.getTrainingRankTotal() + po.getRankVal());
                }
                    break;
            }

            //计算最终得分
            int fullCount = rank.getParentRankCount() + rank.getTrainingRankCount() + rank.getManagerRankCount() + rank.getStudyRankCount();
            int fullTotal = rank.getParentRankTotal() + rank.getTrainingRankTotal() + rank.getManagerRankTotal() + rank.getStudyRankTotal();
            if (fullCount > 0 && fullCount > 0 ){
                rank.setRankVal( (int)( fullTotal * 1.0 / fullTotal));
            }

            rankRepository.save(rank);

            return result;
        }

        return add(areaId, staffId, param, null);
    }

}
